package rs.pnv.util;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class WebServiceClient {

	private static final String MERCHANT_WSDL_LOCATION = "http://localhost:8080/SEP-Merchant/services/Merchant?wsdl";
	private static final String MERCHANT_DEFAULT_NAMESPACE = "http://merchant.pnv.rs/";
	private static final String MERCHANT_SERVICE_NAME = "MerchantImplService";

	private static final String ACQUIRER_WSDL_LOCATION = "http://localhost:8080/SEP-Bank/services/Acquirer?wsdl";
	private static final String ISSUER_WSDL_LOCATION = "http://localhost:8080/SEP-Bank/services/Issuer?wsdl";
	private static final String BANK_DEFAULT_NAMESPACE = "http://bank.pnv.rs/";
	private static final String BANK_SERVICE_NAME = "BankImplService";

	private static final String PCC_WSDL_LOCATION = "http://localhost:8080/SEP-PCC/services/PCC?wsdl";
	private static final String PCC_DEFAULT_NAMESPACE = "http://pcc.pnv.rs/";
	private static final String PCC_SERVICE_NAME = "PccImplService";

	public enum WebServiceType {
		MERCHANT,
		ACQUIRER_BANK,
		ISSUER_BANK,
		PCC
	}

	public static <T> T createWebService(WebServiceType webServiceType, Class<T> interfaceClass) throws MalformedURLException {

		URL wsdlLocation = null;
		QName serviceName = null;

		if (webServiceType == WebServiceType.MERCHANT) {

			wsdlLocation = new URL(MERCHANT_WSDL_LOCATION);
			serviceName = new QName(MERCHANT_DEFAULT_NAMESPACE, MERCHANT_SERVICE_NAME);

		} else if (webServiceType == WebServiceType.ACQUIRER_BANK) {

			wsdlLocation = new URL(ACQUIRER_WSDL_LOCATION);
			serviceName = new QName(BANK_DEFAULT_NAMESPACE, BANK_SERVICE_NAME);

		} else if (webServiceType == WebServiceType.ISSUER_BANK) {

			wsdlLocation = new URL(ISSUER_WSDL_LOCATION);
			serviceName = new QName(BANK_DEFAULT_NAMESPACE, BANK_SERVICE_NAME);

		} else {

			wsdlLocation = new URL(PCC_WSDL_LOCATION);
			serviceName = new QName(PCC_DEFAULT_NAMESPACE, PCC_SERVICE_NAME);

		}

		Service service = Service.create(wsdlLocation, serviceName);
		return service.getPort(interfaceClass);

	}

}
